package com.whw.springboot.walk.controller;

import com.whw.springboot.walk.UseToken;
import com.whw.springboot.walk.config.GlobalConfig;
import com.whw.springboot.walk.entity.Goods;
import com.whw.springboot.walk.entity.Message;
import com.whw.springboot.walk.service.GoodsService;
import com.whw.springboot.walk.utils.ApplyUtil;
import com.whw.springboot.walk.utils.LoadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Api(tags = "失物招领模块")
@RestController
@RequestMapping("/api/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    @Resource
    private GlobalConfig globalConfig;

    @ApiOperation(value = "添加物品")
    @UseToken(level = 0)
    @PostMapping(value = "/insert")
    public Message addGoods(Goods goods){
        String goodsId = "G"+ ApplyUtil.genApplyId();
        goods.setGoodsId(goodsId);
        goods.setGoodsStatus(0);
        goods.setGoodsCheck("0");
        return goodsService.addGoods(goods);
    }

    @ApiOperation(value="上传物品图片")
    @PostMapping(value="/Images" , headers = "content-type=multipart/form-data")
    @UseToken(level = 0)
    public String getImages(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        return globalConfig.hostUrl+"resource/"+ LoadUtil.upload(globalConfig.filePath,files, request);
    }

    @ApiOperation(value = "通过活动id删除物品")
    @DeleteMapping(value = "/delByGoodsId")
    @UseToken(level = 1)
    public Message delGoods(String goodsId){
        return goodsService.delGoods(goodsId);
    }

    @ApiOperation(value = "管理端更新物品信息")
    @PutMapping(value = "/updateGoods")
    @UseToken(level = 1)
    public Message updateGoods(Goods goods){
        return goodsService.updateGoods(goods);
    }

    @ApiOperation(value = "管理端审核物品信息，需要传goodsId,goodsCheck")
    @UseToken(level = 1)
    @PutMapping(value = "/adminManageGoods")
    public Message adminCheckGoods(Goods goods){
        return goodsService.updateGoods(goods);
    }

    @ApiOperation(value = "修改物品状态，需要传goodsId,goodsStatus")
    @UseToken(level = 0)
    @PutMapping(value = "/updateGoodsStatus")
    public Message updateGoodsStatus(Goods goods){
        return goodsService.updateGoods(goods);
    }


    @ApiOperation(value = "根据物品编号查物品信息和发布者信息")
    @UseToken(level = 0)
    @GetMapping(value = "/findOne")
    public Message<Goods> selectOneGoodsByPrimaryKey(String goodsId){
        return goodsService.selectOneGoodsAndUserInfoByPrimaryKey(goodsId);
    }

    @ApiOperation(value = "查询所有的物品信息")
    @UseToken(level = 0)
    @GetMapping(value = "/findAll")
    public Message<List<Goods>> findAllGoods(){
        return goodsService.selectAllGoods();
    }

    @ApiOperation(value = "根据物品名字查询所有的物品信息")
    @UseToken(level = 0)
    @GetMapping(value = "/findByName")
    public Message<List<Goods>> findGoodsByName(String goodsName){
        return goodsService.selectGoodsByName(goodsName);
    }


}
